package com.yantong.controller.platform;

import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.InformationTemplate;
import com.yantong.service.InformationTemplateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


/**
 * 订阅消息
 */
@Controller
@RequestMapping("information/template")
public class InformationTemplateController extends BaseController {

	private static final String prefix = "commander/information";
	@Autowired
	private InformationTemplateService informationTemplateService;

	@RequiresPermissions("commanderList:information:list")
	@GetMapping()
	public String list() {
		return prefix + "/informationList";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils<InformationTemplate> listStorePrinter(String templateName, int offset, int limit) {
		InformationTemplate informationTemplate = new InformationTemplate();
		informationTemplate.setTemplateName(templateName);
		return informationTemplateService.page(informationTemplate, offset, limit);
	}

	@RequiresPermissions("commanderList:information:add")
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}


	@ResponseBody
	@RequiresPermissions("commanderList:information:add")
	@PostMapping("/save")
	public ResultVo save(InformationTemplate informationTemplate ) {
		if (informationTemplateService.insert(informationTemplate) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	@RequiresPermissions("commanderList:information:edit")
	@GetMapping("/edit/{id}")
	public String edit(Model model, @PathVariable Long id) {
		model.addAttribute("information", informationTemplateService.findDetail(id));
		return prefix + "/edit";
	}

	@ResponseBody
	@PostMapping("/update")
	@RequiresPermissions("commanderList:information:edit")
	public ResultVo update(InformationTemplate informationTemplate) {
		if (informationTemplateService.update(informationTemplate) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	@ResponseBody
	@RequiresPermissions("commanderList:information:remove")
	@PostMapping("/remove")
	public ResultVo remove(Long id) {
		if (id == null) {
			return ResultVo.error();
		}
		InformationTemplate informationTemplate = new InformationTemplate();
		informationTemplate.setId(id);
		informationTemplate.setDeleted(1);
		informationTemplateService.update(informationTemplate);
		return ResultVo.ok();
	}

}
